package cn.drenal.capcap.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.drenal.capcap.common.Response;
import cn.drenal.capcap.entity.Comment;
import cn.drenal.capcap.entity.vo.CommentListVO;
import cn.drenal.capcap.service.CommentService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 评论控制器
 */
@RestController
@RequestMapping("/comment")
@RequiredArgsConstructor
public class CommentController {

    private final CommentService commentService;

    /**
     * 创建评论
     *
     * @param comment 评论信息
     * @return 创建结果
     */
    @PostMapping
    @SaCheckPermission("content:comment:add")
    public Response<Comment> createComment(@RequestBody Comment comment) {
        return Response.success(commentService.createComment(comment));
    }

    /**
     * 更新评论
     *
     * @param id 评论ID
     * @param comment 评论信息
     * @return 更新结果
     */
    @PutMapping("/{id}")
    @SaCheckPermission("content:comment:edit")
    public Response<Comment> updateComment(@PathVariable Long id, @RequestBody Comment comment) {
        return Response.success(commentService.updateComment(id, comment));
    }

    /**
     * 删除评论
     *
     * @param id 评论ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    @SaCheckPermission("content:comment:delete")
    public Response<Void> deleteComment(@PathVariable Long id) {
        commentService.deleteComment(id);
        return Response.success();
    }

    /**
     * 根据ID查询评论
     *
     * @param id 评论ID
     * @return 评论信息
     */
    @GetMapping("/{id}")
    @SaCheckPermission("content:comment:query")
    public Response<Comment> getCommentById(@PathVariable Long id) {
        return Response.success(commentService.getCommentById(id));
    }

    /**
     * 分页查询评论列表
     *
     * @param current 当前页
     * @param size 每页大小
     * @param content 评论内容（可选）
     * @param artworkName 作品名称（可选）
     * @param username 用户名（可选）
     * @param status 状态（可选）
     * @return 评论分页列表
     */
    @GetMapping("/page")
    @SaCheckPermission("content:comment:list")
    public Response<Page<CommentListVO>> getCommentPage(
            @RequestParam(defaultValue = "1") long current,
            @RequestParam(defaultValue = "10") long size,
            @RequestParam(required = false) String content,
            @RequestParam(required = false) String artworkName,
            @RequestParam(required = false) String username,
            @RequestParam(required = false) Integer status) {
        Page<Comment> page = new Page<>(current, size);
        return Response.success(commentService.getCommentPage(page, content, artworkName, username, status));
    }
    
    /**
     * 审核评论
     *
     * @param id 评论ID
     * @param status 审核状态：0-待审核，1-已通过，2-已拒绝
     * @return 更新后的评论
     */
    @PutMapping("/{id}/review")
    @SaCheckPermission("content:comment:review")
    public Response<Comment> reviewComment(
            @PathVariable Long id,
            @RequestParam Integer status,
            @RequestParam(required = false) String remark) {
        return Response.success(commentService.reviewComment(id, status, remark));
    }
    
    /**
     * 获取作品的评论列表
     *
     * @param artworkId 作品ID
     * @param current 当前页
     * @param size 每页大小
     * @return 评论分页列表
     */
    @GetMapping("/artwork/{artworkId}")
    public Response<Page<Comment>> getCommentsByArtworkId(
            @PathVariable Long artworkId,
            @RequestParam(defaultValue = "1") long current,
            @RequestParam(defaultValue = "10") long size) {
        Page<Comment> page = new Page<>(current, size);
        return Response.success(commentService.getCommentsByArtworkId(artworkId, page));
    }
    
    /**
     * 点赞评论
     *
     * @param id 评论ID
     * @return 操作结果
     */
    @PutMapping("/{id}/like")
    public Response<Void> likeComment(@PathVariable Long id) {
        commentService.likeComment(id);
        return Response.success();
    }
}